# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : BuiltIn.py
# Time       ：2023/2/19 19:55
# Author     ：Y-aong
# version    ：python 3.7
# Description：
内建函数包含开始结束任务
Built-in functions contain start and end tasks
"""
import time

from pydantic import BaseModel, Field

from conf.config import OrderLinesConfig
from orderlines import OrderLines
from orderlines.libraries.BaseTask import BaseTask
from orderlines.utils.base_orderlines_type import BasePluginParam
from orderlines.utils.language_type import get_desc_by_lang


class DelayParam(BaseModel):
    delay_time: int = Field(description=get_desc_by_lang('delay_time'), default=3)


class SubProcessParam(BasePluginParam):
    sub_process_id: str = Field(description='子流程ID')
    process_instance_id: str = Field(description='父流程实例ID')
    sub_process_params: list = Field(description='子流程参数', default=[])


class BuiltIn(BaseTask):
    version = OrderLinesConfig.version
    title = '基础节点'
    category = '基础节点'
    icon = 'HomeFilled'

    def __init__(self):
        super(BuiltIn, self).__init__()

    def start(self, base_param: BasePluginParam) -> dict:
        """
        开始节点
        start node
        """
        return {'status': self.success}

    def end(self, base_param: BasePluginParam) -> dict:
        """
        结束节点
        end node
        """
        return {'status': self.success}

    def select(self, base_param: BasePluginParam) -> dict:
        """
        标记节点
        select node
        """
        return {'status': self.success}

    def delay(self, delay_time: DelayParam):
        """
        延时节点
        @param delay_time:
        @return:
        """
        time.sleep(int(delay_time.delay_time))
        return {'status': self.success}

    def sub_process(self, param: SubProcessParam):
        """
        子流程
        @param param:
        @return:
        """
        # 获取子流程的变量
        # 获取父流程的变量
        # 将子流程和父流程同名的变量替换为父流程的变量，单独属于子流程的变量依然保留
        OrderLines().start(process_id=param.sub_process_id, process_instance_id=param.process_instance_id)
        return {'status': self.success}
